package com.myapplication.app;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.provider.MediaStore;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.*;
import com.nostra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import org.apache.http.Header;
import org.apache.http.entity.FileEntity;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;


public class MessageImageActivity extends ActionBarActivity implements View.OnClickListener{

    private static final int SELECT_IMAGE = 113;
    private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 112;
    private static final String CACHE_FILE_NAME = "cache_file_name";
    private static final String CACHE_TITLE = "cache_title";
    private static final String CACHE_CONTENT = "cache_content";
    private static final String SP_NAME = "MessageImageActivity";
    private static final String KEY_IMAGE_URI = "imageUri";
    private ImageView imgPreview;
    private Uri imageUri;
    private EditText msgTitle;
    private EditText msgContent;
    private Uri fileUri;
    private static final String TRACK_SAVED_INSTANCE_STATE = "TRACK_SAVED_INSTANCE_STATE";
    private DisplayImageOptions options;
    private ImageLoader imageLoader;
    private Bundle mOut;
    private RelativeLayout mImgContainer;
    private ImageView imageShow;
    private ImageView imagePhoto;
    private ImageView imageCamera;
    private TextView tv_Msg;
    private TextView tv_Me;
    private TextView tv_Bus;
    private int PHOTOCODE=0x123;
    private int CAMERACODE=0x234;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.actvity_image_message);
        imageShow= (ImageView) findViewById(R.id.show);
        imagePhoto= (ImageView) findViewById(R.id.photo);
        imageCamera= (ImageView) findViewById(R.id.camera);
        tv_Msg = (TextView) findViewById(R.id.msg);
        tv_Me = (TextView) findViewById(R.id.me);
        tv_Bus = (TextView) findViewById(R.id.bus);
        imageShow.setOnClickListener(this);
        imagePhoto.setOnClickListener(this);
        imageCamera.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){

            case R.id.photo:
//                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
//                intent.addCategory(Intent.CATEGORY_OPENABLE);
//                intent.setType("image/*");
//                intent.putExtra("crop", "true");
//                intent.putExtra("aspectX",1);
//                intent.putExtra("aspectY",1);
//                intent.putExtra("outputX", 80);
//                intent.putExtra("outputY", 80);
//                intent.putExtra("return-data",true);
//                startActivityForResult(intent, PHOTOCODE);

                Intent i = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(i, PHOTOCODE);
                break;
            case R.id.camera:
                Intent intent2 = new Intent("android.media.action.IMAGE_CAPTURE");
                intent2.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "camera.jpg")));
                intent2.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
                startActivityForResult(intent2,CAMERACODE);
                break;
            case R.id.login:
//                checkRight();
//                loadDate();

                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAMERACODE && resultCode == Activity.RESULT_OK) {
            imageShow.setImageDrawable(Drawable.createFromPath(new File(
                    Environment.getExternalStorageDirectory(), "camera.jpg")
                    .getAbsolutePath()));
        }else if (requestCode == PHOTOCODE && resultCode ==Activity.RESULT_OK) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();
            imageShow.setImageBitmap(BitmapFactory.decodeFile(picturePath));
//            Bitmap cameraBitmap = (Bitmap) data.getExtras().get("data");
//            imageShow.setImageDrawable(new BitmapDrawable(cameraBitmap));
        }
    }


}
